package com.clkj.cldisabledmanclient.mvp.serviceintroduction;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.http.HttpConfig;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;

public class ServiceIntroductionActivity extends BaseActivity implements ServiceIntroductionContract.View {

    private ServiceIntroductionContract.Presenter mPresenter;
    private ProgressDialog progressDialog;
    private WebView wvIntroduction;

    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_introduction);
        initView();
        initData();
        assignView();

        getServiceIntroduction();
    }

    @Override
    public void initData() {

        type = getIntent().getStringExtra("type");
        String toptitle = getIntent().getStringExtra("toptitle");
        initTitle(null, null, toptitle, true, null);
        mPresenter = new ServiceIntroductionPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

        WebSettings webSettings = wvIntroduction.getSettings();

        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); //设置缓存

        webSettings.setJavaScriptEnabled(true);//设置能够解析Javascript

        webSettings.setDomStorageEnabled(true);//设置适应Html5 //重点是这个设置
    }

    @Override
    public void initView() {

        wvIntroduction = (WebView) findViewById(R.id.wv_introduction);
    }

    @Override
    public void getServiceIntroduction() {

        mPresenter.getServiceIntroduction(type);
    }

    @Override
    public void getServiceIntroductionSuccess(String introduction) {

        wvIntroduction.loadDataWithBaseURL(HttpConfig.URL_OF_INTERFACE_BASE, introduction, "text/html; charset=UTF-8", null, null);
    }

    @Override
    public void getServiceIntroductionError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(ServiceIntroductionContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        //设置点击进度对话框外的区域对话框不消失
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

}
